package com.wing.product.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.annotation.RequestObjectFieldIgnore;
import com.wing.common.constant.SecurityConstants;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.product.model.entity.WarehouseOrder;
import com.wing.product.model.entity.WarehouseProductInventory;
import com.wing.product.model.entity.WarehouseTransferOrder;
import com.wing.product.model.request.WarehouseOrderForm;
import com.wing.product.model.request.WarehouseTransferOrderForm;
import com.wing.product.model.response.WarehouseOrderVO;
import com.wing.product.model.response.WarehouseOutboundOrderVO;
import com.wing.product.model.response.WarehouseTransferOrderVO;
import com.wing.product.service.WarehouseProductInventoryService;
import com.wing.product.service.WarehouseTransferOrderService;
import com.wing.product.service.WarehouseTransferOrderServiceImpl;
import com.wing.web.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 过户订单(warehouse_transfer_order)表控制层
 *
 * @author xxxxx
 */


@Api(value = "托管入库管理", tags = "#/warehouse/product/transferOrder")
@RestController("WarehouseTransferOrderController")
@RequestMapping("/warehouse/admin/transferOrder/")
@RequiredArgsConstructor
public class WarehouseTransferOrderController {
    /**
     * 服务对象
     */
    @Autowired
    private WarehouseTransferOrderService warehouseTransferOrderService;


    @ApiOperation(value = "查看过户订单", notes = "搜索")
    @GetMapping("/list")
    public PageResult<WarehouseTransferOrderVO> list(
            @RequestParam(value = "page", defaultValue = "1", required = false) Integer page
            , @RequestParam(value = "limit", defaultValue = "10", required = false) Integer limit
            , @RequestParam(value = "sort", defaultValue = "create_time", required = false) String sort
            , @RequestParam(value = "order", defaultValue = "asc", required = false) String order
            , String warehouseCustomerId
    ) {

        Page<WarehouseTransferOrder> pg = new Page<>(page, limit);
        Map paramMap = new HashMap();
        //判断客户端，如果是会员端，则需要添加对应的部门信息作为查询条件
        if (JwtUtils.getClientId().equals(SecurityConstants.WAREHOUSE_CLIENT_ID)) {
            paramMap.put("warehouseCustomerId", JwtUtils.getDepartmentId());
        } else {
            if (!StringUtils.isEmpty(warehouseCustomerId)) {
                paramMap.put("warehouseCustomerId", warehouseCustomerId);
            }
        }
//        return warehouseTransferOrderService.pageList4app(pg, paramMap);
        IPage<WarehouseTransferOrderVO> list = warehouseTransferOrderService.pageList4app(pg, paramMap);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "添加过户订单", notes = "添加")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/add")
    public JsonResult add(@Validated(WarehouseTransferOrderForm.Add.class) @RequestBody WarehouseTransferOrderForm warehouseTransferOrderForm) {
        try {
            if (JwtUtils.getClientId().equals(SecurityConstants.WAREHOUSE_CLIENT_ID)) {
                warehouseTransferOrderForm.setSellerCustomerId(JwtUtils.getDepartmentId());
            }
            Boolean b = warehouseTransferOrderService.save(warehouseTransferOrderForm);
            if (b) {
                return JsonResult.success("添加成功");
            } else {
                return JsonResult.fail("添加失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.fail(e.getMessage());
        }
    }

    @ApiOperation(value = "受理", notes = "启用")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @GetMapping("/updateStatus")
    public JsonResult updateStatus(Long id, Integer status, String type) {
        return warehouseTransferOrderService.updateStatus(id, status, type);
    }

    @ApiOperation(value = "撤回", notes = "撤回")
    @RequestObjectFieldIgnore({"id"})
    @GetMapping("/retract")
    public JsonResult retract(Long id, Integer status, String type,String  sysBundleCode) {
        return warehouseTransferOrderService.retract(id,status,type,sysBundleCode);
    }



}
